// miniprogram/pages/team_show_page/team_show_page.js

const app = getApp();
const request = require('../../utils/request.js');
const config = require('../../utils/config');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    colorTheme: app.globalData.colorTheme,
    animationTime: 1,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.getTeamPageInfo(options.teamId);
    // this.getTeamPageInfo(1);
  },

  /**
   * 加载团队信息
   */
  getTeamPageInfo: function(teamId) {
    let that = this;
    let url = config.SERVER_URL + 'index/team/' + teamId;
    wx.showLoading({
      title: '加载ing'
    })
    request.requestGetApi(url, {}, {}, 'application/json').then(res=>{
      if(res.status === config.STATUS_OK){
        that.setData({
          teamInfo: res.data
        });
      }
    }).catch(err=>{
      app.failFun(err);
    }).finally(res=>{
      app.completeFun(res);
    });
  },

  /**
   * 申请加入团队(直接申请)
   */
  applyToTeam: function(e){
    if(!app.checkedUserLoading()){
      return;
    }
    let that = this;
    let token = wx.getStorageSync('token');
    let url = config.SERVER_URL + 'teams/userApplyToTeam';
    let teamId = this.data.teamInfo.id;
    let params = {
      teamId: teamId,
      recruitmentId: 0
    };
    wx.showModal({
      title: '提示',
      content: '申请加入该团队？',
      success(res) {
        if (res.confirm) {
          console.log('用户点击确定');
          wx.showLoading({
            title: '发送申请ing',
          })
          request.requestPostApi(url, token, params, 'application/json').then(res=>{
            if(res.status === config.STATUS_OK){
              wx.showToast({
                title: '申请成功~',
                icon: 'success',
                duration: 3000
              });
            }else if(res.status === 400){
              wx.showToast({
                title: res.message,
                icon: 'none',
                duration: 3000
              })
            }
          }).catch(err=>{
            app.failFun(err);
          }).finally(res=>{
            app.completeFun(res);
          });
        } else if (res.cancel) {
          console.log('用户点击取消');
        }
      }
    });
  },

  /**
   * 申请对应的职位
   */
  applyToRecruitment: function(e){
    let that = this;
    let token = wx.getStorageSync('token');
    let url = config.SERVER_URL + 'teams/userApplyToTeam';
    let teamId = this.data.teamInfo.id;
    let recruitmentId = e.currentTarget.dataset.recruitmentid;
    let params = {
      teamId: teamId,
      recruitmentId: recruitmentId
    };
    console.log(params);
    wx.showModal({
      title: '提示',
      content: '申请加入该职位？',
      success(res) {
        if (res.confirm) {
          console.log('用户点击确定');
          wx.showLoading({
            title: '发送申请ing',
          })
          request.requestPostApi(url, token, params, 'application/json').then(res=>{
            if(res.status === config.STATUS_OK){
              wx.showToast({
                title: '申请成功~',
                icon: 'success',
                duration: 3000
              });
            }else if(res.status === 400){
              wx.showToast({
                title: res.message,
                icon: 'none',
                duration: 3000
              });
            }
          }).catch(err=>{
            app.failFun(err);
          }).finally(res=>{
            app.completeFun(res);
          });
        } else if (res.cancel) {
          console.log('用户点击取消');
        }
      }
    });
  },

  /**
   * 查看用户展示页
   */
  goUserShowPage: function(e) {
    let uid = e.currentTarget.dataset.uid;
    wx.navigateTo({
      url: '/pages/user-show-info/user-show-info?uid=' + uid,
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})